package cn.sdormitory.controller.report;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.report.req.DormitoryViolationsStaReq;
import cn.sdormitory.report.service.DormitoryViolationsStaService;
import cn.sdormitory.report.vo.DormitoryViolationsStaVO;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author : yy
 * @date: 2025/4/12 15:55
 * @description:
 */
@RestController
@Api(tags = "宿舍违纪统计")
@RequestMapping("/report/dormitoryViolations")
public class DormitoryViolationsStaController {

    @Resource
    private DormitoryViolationsStaService dormitoryViolationsStaService;

    @ApiOperation("查询宿舍违纪统计列表")
    @PostMapping(value = "/list")
    public CommonResult<CommonPage<DormitoryViolationsStaVO>> list(@RequestBody DormitoryViolationsStaReq dormitoryViolationsStaReq) {
        IPage<DormitoryViolationsStaVO> page = dormitoryViolationsStaService.list(dormitoryViolationsStaReq);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @ApiOperation("导出宿舍违纪统计列表")
    @PostMapping(value = "/export")
    public CommonResult export(HttpServletResponse response, @RequestBody DormitoryViolationsStaReq dormitoryViolationsStaReq) {
        List<DormitoryViolationsStaVO> violationsStaVOS = dormitoryViolationsStaService.export(dormitoryViolationsStaReq);
        try {
            EasyExcel.write(response.getOutputStream(), DormitoryViolationsStaVO.class).sheet("宿舍违纪统计").doWrite(violationsStaVOS);
            return CommonResult.success();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonResult.failed();

    }

}
